// page/category/details.js
Page({
  data: {
    isHide: 0,
    imgUrls: [],
    info: [],
    tat: false,
    tel: '',
    deliveryTime: [],
    mor: '',
    tx: '点击选取规格吧',
    cla: 5
  },
  onLoad: function (c) {
    var app = getApp()
    var that = this;
    var uid = wx.getStorageSync('uid');
    wx.request({
      url: app.globalData.host + '&g=App&m=Category&a=details', //仅为示例，并非真实的接口地址
      data: { uid: uid, id: c.id },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);

        if (res.data.g.id > 0) {
          that.setData({
            imgUrls: res.data.img,
            info: res.data.g,
            tat: res.data.p,
            tel: res.data.t,
            cla: res.data.lx,
            deliveryTime: res.data.xg,

            isHide: 1
          })
          if (res.data.lx == 18 || res.data.lx == 6) {
            that.setData({
              mor: res.data.xg[1]['price'] ? res.data.xg[1]['price'] : res.data.g.price,
            })
          }

        } else {
          wx.showModal({
            title: '提示',
            content: '你查看的商品已经下架啦',
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
      }
    })
  },
  tapcart: function (event) {
    var app = getApp()
    var that = this;
    var openid = wx.getStorageSync('openid');
    var uid = wx.getStorageSync('uid');
    var mor = that.data.mor
    var tx = that.data.tx
    var cla = that.data.cla

    if ((that.data.cla == 6 || that.data.cla == 18) && that.data.tx == '点击选取规格吧') {
      console.log('还未选取规格呢')
      that.openPop()
      return false
    }
    wx.request({
      url: app.globalData.host + '&g=App&m=Category&a=addcart', //仅为示例，并非真实的接口地址
      data: {
        uid: uid,
        gid: event.currentTarget.dataset.id,
        mor: mor,
        tx: tx,
        cla: cla
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        if (res.data.status > 0) {
          wx.switchTab({
            url: '../cart/cart'
          })
        }
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.imgUrls  // 需要预览的图片http链接列表
    })
  },
  pd: function (e) {
    var app = getApp()
    var that = this;
    var uid = wx.getStorageSync('uid');
    var data = that.data;
    if (!that.data.tat) {
      wx.request({
        url: app.globalData.host + '&g=App&m=Category&a=collect', //仅为示例，并非真实的接口地址
        data: { uid: uid, gid: data.info.id },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            tat: true
          })
        }
      })
    } else {
      wx.showToast({
        title: '你已经收藏了',
        image: '/images/icon/close.png',
      });
    }

  }, ontel: function () {
    var that = this;
    var tel = that.data.tel;
    wx.makePhoneCall({
      phoneNumber: tel //仅为示例，并非真实的电话号码
    })
  },
  openPop: function () {
    this.setData({
      showhide: true
    })
  },
  closePop: function () {
    this.setData({
      showhide: false
    })
  },
  selected: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.id
    var arr = that.data.deliveryTime
    var count = 0;
    for (var key in arr) {
      count++;
    }
    for (var i = 1; i <= count; i++) {
      if (i == index) {
        console.log(arr[i].price)
        this.setData({
          mor: arr[i].price,
          tx: arr[i].name,
          showhide: false
        })
        break
      }
    }
  },
  goumai: function () {
    var app = getApp()
    var that = this;
    var mor = that.data.mor
    var tx = that.data.tx
    var cla = that.data.cla
    if (that.data.cla == 6 || that.data.cla == 18) {
      if (that.data.tx == '点击选取规格吧') {
        console.log('还未选取规格呢')
        that.openPop()
        return false
      } else {
        wx.navigateTo({
          url: '/page/cart/order?id=' + that.data.info.id + '&tx=' + tx + '&mor=' + mor + '&cla=' + cla
        })
        return false
      }
    } else {
      wx.navigateTo({
        url: '/page/cart/order?id=' + that.data.info.id
      })
    }
  },
    onShareAppMessage: function (res) {
       var info=this.data.info
        if (res.from === 'button') {
            console.log(res.target)
        }
        return {
            title: info.title,
            path: '/page/category/details?id='+info.id,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    }
})